package com.wk.webview.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.wk.webview.R
import com.wk.webview.utils.Constants
import kotlinx.android.synthetic.main.fragment_webview.view.*

class WebViewFragment : Fragment() {
    private var mLoadService: LoadService<Any>? = null
    private var mUrl: String = ""

    companion object {
        fun newInstance(url: String): WebViewFragment {
            val fragment = WebViewFragment()
            val bundle = Bundle()
            bundle.putString(Constants.WEB_VIEW_URL, url)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mUrl = arguments?.getString(Constants.WEB_VIEW_URL, "") ?: ""
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_webview, container, false)
        mLoadService = LoadSir.getDefault().register(view) {
            view.srl_refresh_layout.isRefreshing = true
            view.wv_webview.reload()
        }
        view.srl_refresh_layout.setOnRefreshListener {
            view.wv_webview.reload()
        }
        return mLoadService!!.loadLayout
    }

}